package com.salim.leetcode.$152;

public class MaximumProductSubarray1 {

    /**
     * 计算每一行的最大值和最小值
     * 每一个数字有两种情况 与上一个相乘以及只使用自己
     * @param nums
     * @return
     */
    public int maxProduct(int[] nums) {
        int max = nums[0];
        int lineMax = nums[0];
        int lineMin = nums[0];
        int temp = lineMax;
        for(int i=1;i<nums.length;i++){
            lineMax = Math.max(nums[i]*lineMin,Math.max(nums[i],nums[i]*temp));
            lineMin = Math.min(nums[i]*lineMin,Math.min(nums[i],nums[i]*temp));
            max = Math.max(max,lineMax);
        }
        return max;
    }

    public static void main(String[] args){
        int[] nums = new int[]{2,3,-2,4};
        MaximumProductSubarray1 maximumProductSubarray1 = new MaximumProductSubarray1();
        maximumProductSubarray1.maxProduct(nums);
    }
}
